package component;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import data.Users;
import winapi251.app.fortpolio.R;

/**
 * 프로필 액티비티
 */
public class ProfileActivity extends Activity
{
	private static final String LOG_TAG = "WAFortpolio_component.ProfileActivity";

	private TextView txtName;
	private TextView txtProfileContent;
	private LinearLayout[] columns;

	private ImageView imgProfile;
	private ImageView imgCoverstory;

	/**
	 * 프로필 액티비티가 생성될 때 호출되는 메서드
	 * @param savedInstanceState : 저장되어 있던 인스턴스 상태
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile);

		if (savedInstanceState == null)
		{
			// 뷰들을 불러온다.
			txtName = (TextView) findViewById(R.id.txt_name);
			txtProfileContent = (TextView) findViewById(R.id.txt_profile_content);
			imgProfile = (ImageView) findViewById(R.id.img_profile);
			imgCoverstory = (ImageView) findViewById(R.id.img_coverstory);

			// 서버로부터 데이터를 읽어와 화면을 갱신한다.
			Users.getInstance().getName(new Users.DataReceiver()
			{
				@Override
				public void onDataReceived(String data, int errorCode)
				{
					if (errorCode == Users.NO_ERROR)
						txtName.setText(data);
				}
			});
			txtProfileContent.setText(Users.getInstance().getProfileContent());

			// 칼럼을 동적으로 추가한다.
			LayoutInflater inflater = getLayoutInflater();
			columns = new LinearLayout[7];
			LinearLayout layout = (LinearLayout) findViewById(R.id.layout_profile_detail);
			for (int i = 0; i < columns.length; i++)
			{
				LinearLayout column = (LinearLayout) inflater.inflate(R.layout.layout_column, null);
				columns[i] = column;
				layout.addView(column);
			}

			// 칼럼을 받아온다.
			for (int i = 0; i < columns.length; i++)
			{
				final TextView txtKey = (TextView) columns[i].findViewById(R.id.txt_key);
				final TextView txtValue = (TextView) columns[i].findViewById(R.id.txt_value);

				Users.getInstance().getColumn(i, new Users.DataReceiver()
				{
					@Override
					public void onDataReceived(String data, int errorCode)
					{
						if (errorCode == Users.NO_ERROR && data != null)
						{
							String[] datas = data.split("\n");
							if (datas.length == 2)
							{
								if (!datas[0].equals(""))
								{
									txtKey.setText(datas[0]);
									txtKey.setVisibility(View.VISIBLE);
								}
								if (!datas[1].equals(""))
								{
									txtValue.setText(datas[1]);
									txtValue.setVisibility(View.VISIBLE);
								}
							}
						}
					}
				});
			}

			imgProfile.setImageBitmap(Users.getInstance().getProfileImage());
			imgCoverstory.setImageBitmap(Users.getInstance().getCoverstoryImage());
		}
	}

	/**
	 * 프로필 액티비티의 버튼을 눌렀을 때 호출되는 메서드
	 * @param view : 눌린 버튼
	 */
	public void onClick(View view)
	{
		switch (view.getId())
		{
		case R.id.btn_back: // 뒤로가기 버튼
			finish();
			break;
		}
	}
}